<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>图片搜索App</title>
    <link rel="stylesheet" href="style.css" />
    <script src="./axios.js"></script>
  </head>

  <body>
    <h1>图片搜索</h1>
    <form>
      <input type="text" id="search-input" placeholder="搜索图片" />
      <button id="search-button">搜索</button>
    </form>
    <div class="search-results">
      <div class="search-result">
        <img
          src="https://plus.unsplash.com/premium_photo-1664361480561-3bdcd6eb3b6f?ixlib=rb-4.0.3&ixid=MnwxMjA3fDB8MHxzZWFyY2h8MXx8bmF0dXJlfGVufDB8fDB8fA%3D%3D&auto=format&fit=crop&w=1000&q=60"
          alt="image"
        />
        <a
          href="https://unsplash.com/photos/g-7X6T7vAI4"
          target="_blank"
          rel="noopener noreferrer"
          >an image of a beach</a
        >
      </div>

      <div class="search-result">
        <img
          src="https://plus.unsplash.com/premium_photo-1664361480561-3bdcd6eb3b6f?ixlib=rb-4.0.3&ixid=MnwxMjA3fDB8MHxzZWFyY2h8MXx8bmF0dXJlfGVufDB8fDB8fA%3D%3D&auto=format&fit=crop&w=1000&q=60"
          alt="image"
        />
        <a
          href="https://unsplash.com/photos/g-7X6T7vAI4"
          target="_blank"
          rel="noopener noreferrer"
          >an image of a beach</a
        >
      </div>
    </div>
    <button id="show-more-button">显示更多</button>
    <script>
      let page = 1;
      let limit = 6;
      let value;
      const input = document.querySelector("#search-input");
      //点击事件收集输入框数据
      document
        .querySelector("#search-button")
        .addEventListener("click", async (e) => {
          e.preventDefault();
          if (input.value) {
            value = input.value;
            // console.log(value);
            await renderImg(value);
          } else {
            alert("输入不能为空!");
          }
        });
      //回车事件搜集数据-函数防抖
      let timerID;
      input.addEventListener("keyup", (e) => {
        e.preventDefault();
        clearTimeout(timerID);
        timerID = setTimeout(async () => {
          if (e.key === "Enter") {
            if (e.target.value) {
              value = e.target.value;
              console.log(value);
              await renderImg(value);
            } else {
              alert("输入不能为空!");
            }
          }
        }, 500);
      });
      //初始数据
      async function searchImg(value) {
        if (value) {
          const imgs = await axios({
            url: "https://jsonplaceholder.typicode.com/photos",
            params: {
              _page: page,
              _limit: limit,
              title_like: value,
            },
          });
          return imgs.data;
        } else {
        }
        // console.log(imgs);
      }
      //渲染图片到页面上
      async function renderImg(value) {
        const imgData = await searchImg(value);
        console.log(imgData);
        if (imgData.length > 0) {
          let imgStr = imgData
            .map((item) => {
              return ` 
        <div class="search-result">
      <img
        src="${item.thumbnailUrl}"
        alt="image" />
      <a href="${item.thumbnailUrl}" target="_blank" rel="noopener noreferrer">${item.title}</a>
    </div>
    `
            })
            .join("");
          document.querySelector(".search-results").innerHTML = imgStr;
        } else {
          document.querySelector(".search-results").innerHTML = "无搜索结果!";
        }
      }
    </script>
  </body>
</html>
